package cc.admin.modules.graduate.service.impl;

import cc.admin.modules.graduate.entity.*;
import cc.admin.modules.graduate.entity.DTO.DesignQuestionsListDto;
import cc.admin.modules.graduate.mapper.*;
import cc.admin.modules.graduate.service.IBlankErrorQuestionsService;
import cc.admin.modules.graduate.service.IChoiceErrorQuestionsService;
import cc.admin.modules.graduate.service.IExamPaperAnswerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 试卷作答
 * @Author: cc-admin
 * @Date: 2025-01-02
 * @Version: V1.0.0
 */
@Service
public class ExamPaperAnswerServiceImpl extends ServiceImpl<ExamPaperAnswerMapper, ExamPaperAnswer> implements IExamPaperAnswerService {

    @Autowired
    ChoiceQuestionsMapper choiceQuestionsMapper;
    @Autowired
    BlankQuestionsMapper blankQuestionsMapper;

    @Autowired
    DesignAnswerListMapper designAnswerListMapper;

    @Autowired
    DesignAnswerMapper designAnswerMapper;

    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;

    @Autowired
    IBlankErrorQuestionsService blankErrorQuestionsService;


    @Override
    public void getPaper(List<ExamPaperAnswer> records) {

        records.forEach(examPaperAnswer -> {
            String choiceIds = examPaperAnswer.getChoiceIds();
            String blankIds = examPaperAnswer.getBlankIds();
            List<String> choiceAnserIdList = Arrays.stream(examPaperAnswer.getChoiceAnswerString().split(",")).collect(Collectors.toList());
            List<String> blankAnserIdList = null;
            if (examPaperAnswer.getBlankAnswerString() != null && examPaperAnswer.getBlankAnswerString() != "") {
                blankAnserIdList = Arrays.stream(examPaperAnswer.getBlankAnswerString().split(",")).collect(Collectors.toList());
                examPaperAnswer.setBlankAnswer(blankAnserIdList);

            }
            if (examPaperAnswer.getBlankResultString() != null && examPaperAnswer.getBlankResultString() != "") {
                ArrayList<String> blankResult = new ArrayList<>();
                for (int i = 0; i < examPaperAnswer.getBlankResultString().length(); i++) {
                    // 截取单个字符
                    String singleChar = examPaperAnswer.getBlankResultString().substring(i, i + 1);
                    blankResult.add(singleChar);
                }
                examPaperAnswer.setBlankResult(blankResult);

            }
            examPaperAnswer.setChoiceAnswer(choiceAnserIdList);
            String designQuestion = examPaperAnswer.getDesignAnswerIds();
            String[] choiceQuestionIds = choiceIds.split(",");
            String[] blankQuestionIds = new String[0];
            if (blankIds != null && blankIds != "") {
                blankQuestionIds = blankIds.split(",");
            }
            String[] designQuestionIds = designQuestion.split(",");
//            String[] blankQuestionIds = new String[0];
//            if (examPaperAnswer.getBlankIds()!=null&&examPaperAnswer.getBlankIds()!="") {
//                blankQuestionIds = examPaperAnswer.getBlankIds().split(",");
//            }
            if (choiceQuestionIds.length != 0) {
                List<String> choiceQuestionIdsList = Arrays.stream(choiceQuestionIds).collect(Collectors.toList());
                List<ChoiceQuestions> choiceQuestionsList = choiceQuestionsMapper.selectBatchIds(choiceQuestionIdsList);
                examPaperAnswer.setChoiceQuestionsList(choiceQuestionsList);
            }
            if (blankQuestionIds.length != 0) {
                List<String> blankQuestionIdsList = Arrays.stream(blankQuestionIds).collect(Collectors.toList());
                List<BlankQuestions> blankQuestionsList = blankQuestionsMapper.selectBatchIds(blankQuestionIdsList);
                examPaperAnswer.setBlankQuestionsList(blankQuestionsList);
            }
            if (designQuestionIds.length != 0) {
                List<String> designQuestionIdsList = Arrays.stream(designQuestionIds).collect(Collectors.toList());
                LambdaQueryWrapper<DesignAnswerList> answerListLambdaQueryWrapper = new LambdaQueryWrapper<>();
                answerListLambdaQueryWrapper.in(DesignAnswerList::getId, designQuestionIdsList);
                answerListLambdaQueryWrapper.orderByAsc(DesignAnswerList::getIndexNum);
                List<DesignAnswerList> DesignQuestionsList = designAnswerListMapper.selectList(answerListLambdaQueryWrapper);
//                List<DesignAnswerList> DesignQuestionsList = designAnswerListMapper.selectBatchIds(designQuestionIdsList);

                DesignQuestionsList.forEach(designAnserListDto -> {
                    //放应用题的子数据
//                    String childIds = designAnserListDto.getChildIds();
//                    List<String> childIdsList = Arrays.stream(childIds.split(",")).collect(Collectors.toList());
//                    List<DesignAnswer> designChildAnswerLists = designAnswerMapper.selectBatchIds(childIdsList);
                    LambdaQueryWrapper<DesignAnswer> queryWrapper = new LambdaQueryWrapper<>();
//                    childIdsList.forEach(childId -> {
//                        queryWrapper.eq(childId!=null&&childId!="",DesignAnswer::getQuestionId,childId);
//                    });
                    queryWrapper.eq(DesignAnswer::getParentId, designAnserListDto.getId());
                    queryWrapper.orderByAsc(DesignAnswer::getIndexNum);


                    List<DesignAnswer> designAnswers = designAnswerMapper.selectList(queryWrapper);
                    //给应用题 答题里放小题
                    designAnserListDto.setQuestionList(designAnswers);
                });
                //试卷里放应用题
                examPaperAnswer.setDesignAnswer(DesignQuestionsList);
            }


        });
    }

    @Override
    @Transactional
    public void updateExamShap(ExamPaperAnswer examPaperAnswer) {
        List<String> blankResult = examPaperAnswer.getBlankResult();
        List<String> blankAnswerList = examPaperAnswer.getBlankAnswer();
        List<BlankQuestions> blankQuestionsList = examPaperAnswer.getBlankQuestionsList();
        StringBuilder sb = new StringBuilder();
        if (blankResult != null) {
            for (int i = 0; i < blankResult.size(); i++) {
                String shape = blankResult.get(i);
                if (!shape.equals("")) {
                    sb.append(shape);
                } else {
                    sb.append(0);
                }
                if (shape.equals("0")) {
                    // 收录填空题错题
                    BlankQuestions blankQuestions = blankQuestionsList.get(i);
                    String blankAnswer = blankAnswerList.get(i);
                    blankErrorQuestionsService.saveErrorBlank(blankQuestions, blankAnswer,examPaperAnswer.getCreateBy());
                }
            }
            String blankResultString = sb.toString();
            examPaperAnswer.setBlankResultString(blankResultString);
        }
        examPaperAnswer.setType("已审批");
        LambdaQueryWrapper<ExamPaperAnswer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaperAnswer::getId, examPaperAnswer.getId());
        examPaperAnswerMapper.update(examPaperAnswer, queryWrapper);
        examPaperAnswer.getDesignAnswer().forEach(designAnswerList -> {
            designAnswerList.getQuestionList().forEach(designAnswer -> {

                String shape = designAnswer.getShape();
                if (shape != null) {
                    LambdaQueryWrapper<DesignAnswer> examPaperAnswerLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    examPaperAnswerLambdaQueryWrapper.eq(DesignAnswer::getId, designAnswer.getId());
                    designAnswerMapper.update(designAnswer, examPaperAnswerLambdaQueryWrapper);
//                    blankErrorQuestionsService.saveDesignAnswer(designAnswerList);
                }
            });
        });


    }
}
